﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 流程委托
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-06-25 
    /// </summary>
    internal class FlowDelegateService : DataFactory, FlowDelegateIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<FlowDelegateEntity>> GetList(string userId, PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<FlowDelegateEntity>().And(m => m.CreatorUserId == userId);
                if (!input.keyword.IsNullOrEmpty())
                {
                    queryWhere = queryWhere.And(m => m.FlowName.Contains(input.keyword) || m.FlowCategory.Contains(input.keyword));
                }
                return await db.GetIQueryable<FlowDelegateEntity>().Where(queryWhere).OrderByDescending(t => t.CreatorTime).GetPageResultAsync(input);

            }
        }

        public async Task<List<string>> GetUser(string userId)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<FlowDelegateEntity>().And(m => m.ToUserId == userId && m.EndTime > DateTime.Now);
                var data = await db.GetIQueryable<FlowDelegateEntity>().Where(queryWhere).OrderByDescending(t => t.CreatorTime).ToListAsync();
                return data.Select(x => x.CreatorUserId).ToList();
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<FlowDelegateEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<FlowDelegateEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(FlowDelegateEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(FlowDelegateEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(FlowDelegateEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }
    }
}